<?php

require_once 'config.php';

//Category file headers
$category_header_array = array('categoryid',
                        'parentid',
                        'categoryname',
                        'categoryorder',
                        'categoryvisible',
                        'alternateurl',
                        'lastmodified',
                        'category_displaymode',
                        'subcategory_displaymode1',
                        'subcategory_displaymode2',
                        'display_columns',
                        'display_rows',
                        'display_showlistprice',
                        'display_showsaleprice',
                        'display_showyousave',
                        'display_showdescriptionshort',
                        'display_showdescription',
                        'display_showstockstatus',
                        'display_showavailability',
                        'display_featuredproductsonly',
                        'subcategory_displaymode2_columns',
                        'rootid',
                        'metatag_title',
                        'metatag_description',
                        'default_sortby',
                        'link_title_tag',
                        'lastmodby',
                        'category_graphic_placement',
                        'subcategory_displaycolumns',
                        'filtercategory',
                        'auto_maintained_optioncatid',
                        'auto_maintained_optionid',
                        'private_section_customers_only',
                        'categorydescriptionshort',
                        'categorydescription',
                        'metatag_keywords',
                        'categorydescription_belowproducts',
                        'custom_where_clause',
                        'custom_metatags_override',
                        'alternateurl_opennewwindow',
                        'hidden',
                        'display_showpoints',
                        'vcompareoptout',
                        'breadcrumb',
                        'photoseed');

$category_header_string = "\"";

$category_header_string .= implode('","', $category_header_array);

$category_header_string.="\"".Line_Terminator;


$categorytemplate = new categoryClass();
$complete_category_list = array(); //a list holding all the categories including the parents

//get a list of the distinct categories 

$query = "Select distinct categoryid from product_temp where categoryid in (4821,4871,4810,4876,10321)";
$result = query($query);


//get all the categories that qualify (parents) as per the defined list of categories
while($row = mysql_fetch_assoc($result)){
    $categorytree = explode('>', $categorytemplate->get_category_tree($row['categoryid'], FALSE, '>'));
    foreach($categorytree as $category)
    {
        $complete_category_list[$category]=(int)$category;
    }
}

foreach($complete_category_list as $categorykey=>$categoryvalue){
    $tree = explode('>', $categorytemplate->get_category_tree($categorykey, FALSE, '>'));
        if(count($tree)>1)
        {
            //if it's not a topmost category Id
            $category_array = array_reverse($tree);
            $parentId = $category_array[1];
        }
        else
        {
            $parentId = 0; //it's a topmost category
        }
    $categoryName = $categorytemplate->get_category_name($categorykey);
    $rootCategoryId = $tree[0];
    $breadCrumbs = explode('>', $categorytemplate->get_category_tree($categorykey, TRUE, '>'));
        if(count($breadCrumbs)>1)
        {
            array_pop($breadCrumbs);// remove the category representing the curent category
            $breadCrumbs = implode('>', $breadCrumbs);
        }
        else
        {
            $breadCrumbs = ""; //root categories do not need to have breadcrumbs 
        }
        
    $category_rows .=   $categorykey.Delimiter.
                        $parentId.Delimiter. //volusion requires the parentcategoryid to be 0 for the topmost categories
                        '"'.cleanup($categoryName).'"'.Delimiter.
                        '""' .Delimiter.      //Category Order left blank
                        "1".Delimiter.     //category visible
                        '""'.Delimiter.    //alternate URL
                        '""'.Delimiter.    //last modified 
                        "6".Delimiter.     //Category Diaplay Mode
                        '""'.Delimiter.    //Sub-Category Displaymode
                        '""'.Delimiter.    //Sub-Category Displaymode 2
                        "3".Delimiter.     //Display Columns (for products)
                        "2".Delimiter.     //Display Rows (for Product)
                        '"Y"'.Delimiter.    //Display Show list price Y/N
                        '"Y"'.Delimiter.    //Display show sale price Y/N
                        '"N"'.Delimiter.    //Display show you save Y/N
                        '"Y"'.Delimiter.     //Display show Short Description (for products)
                        '"Y"'.Delimiter.     //Display show Description (For Products)
                        '"N"'.Delimiter.     //Display show stock status
                        '"N"'.Delimiter.     //Display show availability 
                        '"N"'.Delimiter.     //Display featured products only
                        '""'.Delimiter.    //Sub-Category display mode 2 columns
                        $rootCategoryId.Delimiter. //Root Category
                        '"'.cleanup($categoryName).'"'.Delimiter. //MetaTag title for category
                        '"'.cleanup($categoryName).'"'.Delimiter. //MetaTag Description for category
                        '"Title"'.Delimiter.    //Default sort by
                        '"'.cleanup($categoryName).'"'.Delimiter. //Link Title Tag
                        '""'.Delimiter.         //Last modified by - optional - leaving blank
                        '""'.Delimiter.        //Graphic placement - optional - leaving blank
                        '""'.Delimiter.      //Sub-Category Display columns - optinal - leaving blank
                        '"N"'.Delimiter.   //Filter Category Y/N
                        '""'.Delimiter.    //Automaintained option CatID - Optional - leave blank
                        '""'.Delimiter.    //Automaintained option ID - Optional - leave blank
                        '"N"'.Delimiter.    //private section customers only - set to N - Y/N
                        '""'.Delimiter.     //Category description short - Optional - leave blank
                        '""'.Delimiter.     //Category description - Optional - leave blank
                        '""'.Delimiter.     //Meta Keywords - Optional - leave blank
                        '""'.Delimiter.     //Category Description below products - Optional - leave blank
                        '""'.Delimiter.     //Custom where clause - Optional - leave blank
                        '""'.Delimiter.     //Custom Mata tags over-ride - Optional - leave blank
                        '"N"'.Delimiter.     //Alternate URL open new window - Y/N
                        '"N"'.Delimiter.     //Hidden - Y/N
                        '""'.Delimiter.     //Display Show points - Optional - Leave blank
                        '"N"'.Delimiter.     //Vcompare Opt out - Y/N
                        '"'.$breadCrumbs.'"'.Delimiter.   //Breadcrumbs
                        '""'.Delimiter.Line_Terminator;     //photoseed - Optional - leave blank
}

$categoryContent = $category_header_string.$category_rows;
//Output Category file
$file = new FileWriter("volusion_category.csv", Output_File_Directory, 'a', TRUE);
$file->write($categoryContent);
$file->close_handle();


//Product File

$product_header_array = array('productcode',        //Unique Identifier
                        'vendor_partno',    //MPN
                        'productname',      //Product Description Type 2
                        'stockstatus',      //Inventory
                        'upc_code',         //UPC
                        'productkeywords',  //Keywords
                        'productnameshort', //Product Description Type 2
                        'productweight',    //Product Weight
                        'productprice',     //The actual price of the product
                        'listprice',        //MSRP
                        'saleprice',        //Sale price if it's a discounted price    
                        'taxableproduct',   //Y/N field
                        'productcondition', //New or specified value
                        'productmanufacturer',// Manufacturer
                        'vendor_price',     //Cost of the product
                        'photourl_small',   //Thumbnail Image
                        'photourl_large',   //Large Image
                        'productdescriptionshort',//Short Description Type 2
                        'productdescription',//Full Description 
                        'productfeatures',  //Product Overview
                        'techspecs',        //Technical Specs
                        'metatag_keywords', //Keywords
                        'categoryids',      //Category Ids (Child including parents)
                        'accessories',      //Product codes for Accessory products
                        'ProductDescription_AbovePricing');    //A description above the price 

$product_header_string = '"';
$product_header_string .= implode('","', $product_header_array);
$product_header_string .= "\"".Line_Terminator;


$query = "Select p.productid productid,categoryid,mfgpartno,upc.sku as upc from product_temp p
          left join productskus upc on p.productid = upc.productid and upc.name = 'UPC' and upc.localeid = 1
            where categoryid in (4821,4871,4810,4876,10321)";
$result = query($query);
$productContent = "";

while ($row = mysql_fetch_assoc($result)) {
    global $productContent;
    global $categorytemplate;
    
    $cda = $categorytemplate->getCategoryDisplayAttributes($row['categoryid']);
    
    $product = new product($row['productid'], $cda, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE);
    $product->price = 1;
    $product->saleprice = 2;
    $product->listprice = 3;
    
    
    $productContent .=     $row['productid'].',"'.
                    $row['mfgpartno'].'","'.
                    cleanup($product->get_description(2)).'",'.
                    '10,"'.
                    $row['upc'].'","'.
                    cleanup(truncate_words($product->get_keywords(),200)).'","'.
                    cleanup(truncate_words($product->get_description(2),50)).'","'.
                    '10",'.
                    $product->price.','.
                    $product->listprice.','.
                    $product->saleprice.',"'.
                    'Y","'.
                    '"New,"'.
                    $product->get_attribute(3411896).'",'.
                    '1,"'.
                    $product->get_resource("160", "", FALSE, TRUE).'","'.
                    $product->get_resource("300", "", FALSE, TRUE).'","'.
                    cleanup($product->get_description(2)).'","'.
                    cleanup($product->get_description(1)).'","'.
                    cleanup($product->get_product_overview()).'","'.
                    cleanup($product->get_techspecs()).'","'.
                    cleanup(truncate_words($product->get_keywords(),200)).'","'.
                    $categorytemplate->get_category_tree($row['categoryid'], FALSE, ',').'","'.
                    "\",\"<b>FOR DEMO PURPOSE ONLY - NOT ACTUAL PRICE</b>\"".Line_Terminator;
    
}

$file = new FileWriter("volusion_product.csv", Output_File_Directory, 'a', TRUE);
$file->write($product_header_string. trim($productContent,Line_Terminator));
$file->close_handle();

?>

</html>